package com.code2roc.dbm.framework.context;

import com.code2roc.dbm.framework.database.MapperBeanManage;
import com.code2roc.dbm.framework.datadic.DataDicManage;
import com.code2roc.dbm.framework.util.FastJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
@Order(value = 10)
public class StartUpContext implements ApplicationRunner {
    @Autowired
    private ApplicationContext context;
    @Autowired
    private MapperBeanManage mapperBeanManage;
    @Autowired
    private DataDicManage dataDicManage;



    @Override
    public void run(ApplicationArguments args) throws Exception {
        Map<String, Object> annotatedBeans = context.getBeansWithAnnotation(SpringBootApplication.class);
        Class bootClass = annotatedBeans.values().toArray()[0].getClass();
        //扫描dao
        mapperBeanManage.initMapperBean(bootClass);
        //扫描数据字典
        dataDicManage.initCodeItem(bootClass);
        //jsonfield个性序列化时间格式
        FastJsonUtil.scanDate2Json(bootClass);
        //执行自定义系统启动事件
        Map<String, ISystemStartExecute> res = context.getBeansOfType(ISystemStartExecute.class);
        for (Map.Entry en :res.entrySet()) {
            ISystemStartExecute service = (ISystemStartExecute)en.getValue();
            service.execute();
        }
    }
}
